package com.ymatou.iapi.live.testcase;

import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.live.parameter.GetSellerCouponListBean;
import com.ymatou.iapi.live.service.GetSellerCouponListCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.UserCallService;
import com.ymttest.database.model.YmtCouponBatch;
import com.ymttest.database.model.YmtCouponPrivateUserBound;
import com.ymttest.database.model.YmtCouponSetting;
import com.ymttest.database.model.YmtCouponValue;
import com.ymttest.database.sqlwapper.YmtCouponBatchWapper;
import com.ymttest.database.sqlwapper.YmtCouponPrivateUserBoundWapper;
import com.ymttest.database.sqlwapper.YmtCouponSettingWapper;
import com.ymttest.database.sqlwapper.YmtCouponValueWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;

public class Ts_GetSellerCouponList {
	
	private static GetSellerCouponListBean getSellerCouponListBean;
	private static GetSellerCouponListCall getSellerCouponListCall;
	
	private static int sellerId = Integer.parseInt(EnvSetup.getData("zhangyi_seller002_id"));

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取买手优惠券列表");
	}

	@Before
	public void caseUp() throws Exception {
		getSellerCouponListBean = new GetSellerCouponListBean();
		getSellerCouponListCall = new GetSellerCouponListCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getSellerCouponListCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@SuppressWarnings("rawtypes")
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetSellerCouponList_001() {
		 Logger.start(true,"验证接口数据是否正常");
		 try {
			 Map user = UserCallService.registerUser(TestDataManager.getEmail(), null,null);
			 
			 int userId = (Integer)user.get("userId");
			 getSellerCouponListBean.setSellerId(sellerId);
			 getSellerCouponListBean.setUserId(userId);
			 
			 getSellerCouponListCall.setData(getSellerCouponListBean);
			 getSellerCouponListCall.callService();
			 
			 JSONArray couponList = new JSONArray(getSellerCouponListCall.getCouponsList().toString());

			 int iCouponSettingId = 0;
			 
			 for (int i = 0; i < couponList.length(); i++) {
				 Logger.comment("开始验证第" + i + 1 + "个couponitem");
				 JSONObject couponItem = couponList.getJSONObject(i);
				 int batchId = couponItem.getInt("id");
				 Logger.verifyEquals(couponItem.getInt("state"), 0, "验证数据库领取状态与接口是否一致");
				 List<YmtCouponBatch> list = new YmtCouponBatchWapper().selectBysBatchCode(String.valueOf(batchId));
				 
				 for (YmtCouponBatch item : list) {
					 iCouponSettingId = item.getiCouponSettingId();
					 List<YmtCouponPrivateUserBound> userBoundList = new YmtCouponPrivateUserBoundWapper().selectByBatchIdAndUserId(batchId, 4085);
					 List<YmtCouponValue> valuelist = new YmtCouponValueWapper().selectByiCouponSettingId(iCouponSettingId);
					 List<YmtCouponSetting> settinglist = new YmtCouponSettingWapper().selectByiCouponSettingId(iCouponSettingId);
					 
					 if (userBoundList.size() > 0)
						 Logger.verifyEquals(couponItem.getInt("state"), 1, "验证数据库领取状态与接口是否一致");
					 else
						 Logger.verifyEquals(couponItem.getInt("state"), 0, "验证数据库领取状态与接口是否一致");
					 
					 Logger.verifyEquals(couponItem.getInt("sellerId"), item.getiOperatorId(), "验证数据库sellerId与接口是否一致");
					 Logger.verifyEquals(batchId, Integer.valueOf(item.getsBatchCode()), "验证数据库batchid与接口是否一致");
					 
					 
					 
					 for (YmtCouponValue valueItem : valuelist) {
							Logger.verifyEquals(couponItem.getString("description"), 
									"满" + valueItem.getFminordervalue().toBigInteger() + "减" + valueItem.getFcouponvalue().toBigInteger(), "验证数据库满减金额与接口是否一致");
							Logger.verifyEquals(couponItem.getDouble("amount"), valueItem.getFcouponvalue().doubleValue(), "验证数据库优惠券金额与接口是否一致");
							Logger.verifyEquals(couponItem.getDouble("amount"), valueItem.getFcouponvalue().doubleValue(), "验证数据库优惠券金额与接口是否一致");
					 }
					 
					 for (YmtCouponSetting settingItem : settinglist) {
							Logger.verifyEquals(couponItem.getLong("expireTime"), settingItem.getdValidEnd().getTime(), "验证数据库优惠券过期时间与接口是否一致");
							Logger.verifyEquals(couponItem.getInt("unReceiveCount"), this.getUnReceiveCount(userBoundList.size(), 
									settingItem.getiMaxUseTimePerUser(), settingItem.getiMaxUseTime(), settingItem.getiReceiveCount()), "验证数据库优惠券可领用次数与接口是否一致");
					 }
				 }
			 }
		 } catch (Exception e) {
			 Logger.fail(e);
		 }
	 }
	
	/**
	 * 
	 * 获取用户可领取优惠券的数量
	 * @param userReceived(用户已经领取的数量)，iMaxUseTimePerUser(每个用户的最大领取数量)， iMaxUseTime（优惠券最大数量）， iReceiveCount（优惠券已经领取的数量）
	 * @return Integer
	 * @throws JSONException 
	 * @throws Exception
	 */
	public int getUnReceiveCount(int userReceived, int iMaxUseTimePerUser, int iMaxUseTime, int iReceiveCount) {
		int count = iMaxUseTime - iReceiveCount;
		int total = iMaxUseTimePerUser - userReceived;
		
		if (iMaxUseTime == iReceiveCount)
			return 0;
		
		if (iMaxUseTimePerUser == userReceived)
			return 0;
		
		if (count >= iMaxUseTimePerUser) {
			return total;
		} else {
			if (total >= count)
				return count;
			else
				return total;
		}
	}
}
